//
//  CYMoreViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/30.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYMoreViewController.h"
#import "CYDataSourceDelegate.h"
#import "CYPassWordViewController.h"
#import "CYUserCell.h"
#import "CYUserHeader.h"
@interface CYMoreViewController ()<CYUserCellDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) CYButton *logoutBtn;
@property (nonatomic, strong) CYDelegate *delegate;
@property (nonatomic, strong) CYDataSource *datasource;
@end

@implementation CYMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview.hidden = self.logoutBtn.hidden = NO;
    // Do any additional setup after loading the view.
}

- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]init];
        _tableview.scrollEnabled = NO;
        [self.view addSubview:_tableview];
        [self setupTableview];
        [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(NAVIBAR_HEIGHT + STATUS_HEIGHT);
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
        }];
    }
    return _tableview;
}

- (CYButton *)logoutBtn{
    if (!_logoutBtn) {
        _logoutBtn = [[CYButton alloc]initWithTitle:@"退出" titleColor:ORANGE_COLOR backgroundColor:LINE_COLOR cornerRadius:5.0];
        [_logoutBtn addTarget:self action:@selector(actionForLogoutBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_logoutBtn];
        
        [_logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view).with.offset(- AL(20));
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(40), AL(44)));
        }];
    }
    return _logoutBtn;
}

- (void)setupTableview{
    WS(self)
    _datasource = [[CYDataSource alloc]initWithData:[self getCellTitle] identifier:@"CYUserCell" block:^(id cell, id data) {
        [weakSelf bindCell:cell dict:data];
    }];
    _delegate = [[CYDelegate alloc]initWithCellHeight:AL(PT(88.0)) views:[self setupCellHeader] block:^(NSIndexPath *index) {
        [weakSelf selectCell:index];
    }];
    _tableview.delegate = _delegate;
    _tableview.dataSource = _datasource;
}

- (void)bindCell:(CYUserCell *)cell dict:(NSDictionary *)dict{
    cell.title = dict[@"title"];
    cell.onStr = dict[@"switch"];
    cell.delegate = self;
}

- (void)selectCell:(NSIndexPath *)index{
    
    if (index.section == 1){
        if (index.row == 1 && ![CYUser shareUser].bankName) {
            [CYHUD show:@"请先绑定银行卡"];
            return;
        }
        CYPassWordViewController *pwdVC = [[CYPassWordViewController alloc]init];
        pwdVC.type = (index.row == 0)?LoginPasswordType:MoneyPasswordType;
        [self.navigationController pushViewController:pwdVC animated:YES];
    }
    else if (index.section == 2){
        NSString * vcName = @"CYCompanyViewController";
        Class vc = NSClassFromString(vcName);
        [self.navigationController pushViewController:[vc new] animated:YES];
    }
}

#pragma mark - CYUserCellDelegate事件
-(void)actionForCYUserCell:(BOOL)on cellTitle:(NSString *)title{
    if ([title containsString:@"摇一摇"])
        [CYAudioManager shareCYAudioManager].openAlert = on;
    else if([title containsString:@"触摸音效"]){
        
    }
    
}

//退出事件
- (void)actionForLogoutBtn{
    [CYAlert showInWindowWithTitle:@"警告" message:@"是否退出登录!" enterComplete:^{
        [CYUser shareUser].username = nil;
        [CYUser shareUser].token = nil;
        Class VC = NSClassFromString(@"CYLoginViewController");
        [self.navigationController pushViewController:[VC new] animated:YES];
    }];
    
}

-(NSArray *)setupCellHeader{
    return @[@{@"headerView":[CYUserHeader lightgrayView],@"headerHeight":@(AL(PT(20)))},
             @{@"headerView":[CYUserHeader lightgrayView],@"headerHeight":@(AL(PT(20)))},
             @{@"headerView":[CYUserHeader lightgrayView],@"headerHeight":@(AL(PT(20)))}];
}

- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationItem.title = @"更多设置";
}

- (NSArray *)getCellTitle{
    NSArray *array = @[@[@{@"title":@"开启摇一摇",@"switch":([[CYAudioManager shareCYAudioManager] openAlert])?@"YES":@"NO"},
                         @{@"title":@"开启触摸音效",@"switch":@"NO"}],
                       @[@{@"title":@"修改登录密码"},
                         @{@"title":@"修改提现密码"}],
                       @[@{@"title":@"关于我们"}]];
    
    return array;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
